import {MigrationInterface, QueryRunner} from "typeorm";

export class Test1600062412231 implements MigrationInterface {
    name = 'Test1600062412231'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query("CREATE TABLE `userExtend` (`id` int NOT NULL AUTO_INCREMENT COMMENT '主键id', `qq` varchar(255) NULL COMMENT 'qq账号', `address` varchar(255) NULL COMMENT '地址', `userId` int NULL COMMENT '主键id', UNIQUE INDEX `REL_66212e7c38b6fc8f66603adc77` (`userId`), PRIMARY KEY (`id`)) ENGINE=InnoDB");
        await queryRunner.query("CREATE TABLE `role` (`id` int NOT NULL AUTO_INCREMENT COMMENT '主键id', `title` varchar(11) NOT NULL COMMENT '身份角色', `is_del` tinyint NOT NULL COMMENT '是否可以删除，0-可以，1-不可以' DEFAULT 0, `create_time` timestamp(6) NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6), `update_time` timestamp(6) NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6), UNIQUE INDEX `IDX_4a74ca47fe1aa34a28a6db3c72` (`title`), PRIMARY KEY (`id`)) ENGINE=InnoDB");
        await queryRunner.query("CREATE TABLE `user` (`id` int NOT NULL AUTO_INCREMENT COMMENT '主键id', `username` varchar(64) NULL COMMENT '用户姓名', `age` int NULL COMMENT '年龄', `is_del` tinyint NOT NULL COMMENT '是否可删除，0-可以，1-不可以' DEFAULT 0, `create_time` timestamp(6) NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6), `update_time` timestamp(6) NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6), PRIMARY KEY (`id`)) ENGINE=InnoDB");
        await queryRunner.query("CREATE TABLE `posts` (`id` int NOT NULL AUTO_INCREMENT COMMENT '主键id', `title` varchar(11) NOT NULL COMMENT '文章标题', `content` varchar(300) NULL COMMENT '文章内容', `is_del` tinyint NOT NULL COMMENT '是否可以删除，0-可以，1-不可以' DEFAULT 0, `create_time` timestamp(6) NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6), `update_time` timestamp(6) NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6), `userId` int NULL COMMENT '主键id', UNIQUE INDEX `IDX_2d82eb2bb2ddd7a6bfac8804d8` (`title`), PRIMARY KEY (`id`)) ENGINE=InnoDB");
        await queryRunner.query("CREATE TABLE `user_role` (`roleId` int NOT NULL, `userId` int NOT NULL, INDEX `IDX_dba55ed826ef26b5b22bd39409` (`roleId`), INDEX `IDX_ab40a6f0cd7d3ebfcce082131f` (`userId`), PRIMARY KEY (`roleId`, `userId`)) ENGINE=InnoDB");
        await queryRunner.query("ALTER TABLE `userExtend` ADD CONSTRAINT `FK_66212e7c38b6fc8f66603adc772` FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION");
        await queryRunner.query("ALTER TABLE `posts` ADD CONSTRAINT `FK_ae05faaa55c866130abef6e1fee` FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION");
        await queryRunner.query("ALTER TABLE `user_role` ADD CONSTRAINT `FK_dba55ed826ef26b5b22bd39409b` FOREIGN KEY (`roleId`) REFERENCES `role`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION");
        await queryRunner.query("ALTER TABLE `user_role` ADD CONSTRAINT `FK_ab40a6f0cd7d3ebfcce082131fd` FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION");
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query("ALTER TABLE `user_role` DROP FOREIGN KEY `FK_ab40a6f0cd7d3ebfcce082131fd`");
        await queryRunner.query("ALTER TABLE `user_role` DROP FOREIGN KEY `FK_dba55ed826ef26b5b22bd39409b`");
        await queryRunner.query("ALTER TABLE `posts` DROP FOREIGN KEY `FK_ae05faaa55c866130abef6e1fee`");
        await queryRunner.query("ALTER TABLE `userExtend` DROP FOREIGN KEY `FK_66212e7c38b6fc8f66603adc772`");
        await queryRunner.query("DROP INDEX `IDX_ab40a6f0cd7d3ebfcce082131f` ON `user_role`");
        await queryRunner.query("DROP INDEX `IDX_dba55ed826ef26b5b22bd39409` ON `user_role`");
        await queryRunner.query("DROP TABLE `user_role`");
        await queryRunner.query("DROP INDEX `IDX_2d82eb2bb2ddd7a6bfac8804d8` ON `posts`");
        await queryRunner.query("DROP TABLE `posts`");
        await queryRunner.query("DROP TABLE `user`");
        await queryRunner.query("DROP INDEX `IDX_4a74ca47fe1aa34a28a6db3c72` ON `role`");
        await queryRunner.query("DROP TABLE `role`");
        await queryRunner.query("DROP INDEX `REL_66212e7c38b6fc8f66603adc77` ON `userExtend`");
        await queryRunner.query("DROP TABLE `userExtend`");
    }

}
